package cae.gui.chat;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import cae.gui.util.CaeImage;
import cae.gui.util.CaeJDialog;
import cae.logic.chat.CaeChatManager;
import cae.logic.chat.CaeChatMessage;
import cae.logic.destination.CaeDestination;
import cae.logic.destination.CaeDestinatiosManager;

import com.jeans.trayicon.WindowsTrayIcon;

public class CaeChatDialog extends CaeJDialog implements ActionListener, Observer
{
	private static final long serialVersionUID = 1L;
	private JPanel panel = null;
	
	private JTextArea textArea;
	private final JTextField inputText = new JTextField();
	private final CaeChatManager logic;
	//private static Logger logger = Logger.getLogger(CaeChatDialog.class);
	private final WindowsTrayIcon iconTray;
	
	private CaeChatDialog dialog;
	
	public CaeChatDialog(JFrame frame, CaeChatManager logicp, WindowsTrayIcon iconTrayP)
	{
		super(frame, "Chat", true);
		
		dialog = this;
		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		this.logic = logicp;
		this.iconTray = iconTrayP;
		
		addWindowListener(new WindowListener()
		{
			public void windowActivated(WindowEvent arg0) {
				CaeImage.setIcon(iconTray, CaeImage.DEFAULT);
			}
			public void windowClosed(WindowEvent arg0) {}
			public void windowClosing(WindowEvent arg0) {
				dialog.logic.deleteObserver(dialog);
				dispose();
			}
			public void windowDeactivated(WindowEvent arg0) {}
			public void windowDeiconified(WindowEvent arg0) {}
			public void windowIconified(WindowEvent arg0) {}
			public void windowOpened(WindowEvent arg0) {
				
				List<CaeChatMessage> msgs = logic.getLastMessages();
				for (CaeChatMessage message : msgs) {
					printMessage(message);
				}
				
				CaeImage.setIcon(iconTray, CaeImage.DEFAULT);
				dialog.requestFocusInWindow();
				inputText.requestFocus();
				
			}
			
		});
		
		logic.addObserver(this);
		
		panel = new JPanel();
		getContentPane().add(panel);
		panel.setLayout( new GridBagLayout() );
		GridBagConstraints destConst = new GridBagConstraints();
		destConst.insets = new Insets(2,2,2,2);
		destConst.fill = GridBagConstraints.BOTH;
		destConst.weightx = 100;
		destConst.weighty = 100;
		
		textArea = new JTextArea(10, 40);
		textArea.setLineWrap(true);
		textArea.setEditable(false);
		
		destConst.gridx = 0;
		destConst.gridy = 0;
		panel.add(new JScrollPane(textArea), destConst);

		inputText.addKeyListener(new KeyListener()
		{
			public void keyPressed(KeyEvent e) {
			}
			public void keyReleased(KeyEvent e) {
				if (e.getKeyCode() == 10)
				{
					sendAndClean();
				}
			}
			public void keyTyped(KeyEvent e) {
			}
			
		});
		destConst.gridx = 0;
		destConst.gridy = 1;
		destConst.weighty = 0;
		panel.add(inputText, destConst);
		
		JButton sendB = new JButton("Enviar");
		sendB.addActionListener(this);
		destConst.gridx = 0;
		destConst.gridy = 2;
		panel.add(sendB, destConst);
		
		pack();
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation(screenSize.width / 4, screenSize.height /4);
        
		setResizable(true);
		setAlwaysOnTop(true);
		
		
		
		
		// TODO FOCO
		//FocusManager.getCurrentManager().
		
	}

	public void actionPerformed(ActionEvent e)
	{
		sendAndClean();
	}
	
	String sentText;
	public void sendAndClean()
	{
		sentText = inputText.getText();
		new Thread(new Runnable()
		{
			public void run() 
			{
				if(!sentText.isEmpty())
					CaeChatManager.sendMessage(sentText);
			}
		}).start();
		inputText.setText("");
	}


	public void update(Observable o, Object arg) {
		
		CaeChatMessage msg = (CaeChatMessage)arg; 
		
		printMessage(msg);
	}
	
	public void printMessage(CaeChatMessage msg)
	{
		CaeDestination fromDest = CaeDestinatiosManager.getInstance().getDestination(msg.getFrom());
		
		if(fromDest != null)
			textArea.append(fromDest.getAlias() + ": " + msg.getText()+"\n");
		else
			textArea.append(msg.getFrom() + ": " + msg.getText()+"\n");
		
		textArea.setCaretPosition(textArea.getDocument().getLength());
	}
}

